namespace Asmex.ObjViewer
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    public class ObjViewer : UserControl
    {
        private ColumnHeader colName;
        private ColumnHeader colValue;
        private Container components;
        private Label lblKind;
        private Label lblName;
        private ListView lvProps;

        public ObjViewer()
        {
            this.InitializeComponent();
        }

        private void AddProp(string k, string v)
        {
            this.lvProps.Items.Add(new ListViewItem(new string[] { k, v }));
        }

        private void Clear()
        {
            this.lblName.Text = "";
            this.lblKind.Text = "";
            this.lvProps.Items.Clear();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.lvProps = new ListView();
            this.colName = new ColumnHeader();
            this.colValue = new ColumnHeader();
            this.lblName = new Label();
            this.lblKind = new Label();
            base.SuspendLayout();
            this.lvProps.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lvProps.Columns.AddRange(new ColumnHeader[] { this.colName, this.colValue });
            this.lvProps.FullRowSelect = true;
            this.lvProps.GridLines = true;
            this.lvProps.Location = new Point(0, 0x48);
            this.lvProps.Name = "lvProps";
            this.lvProps.Size = new Size(0x128, 0x128);
            this.lvProps.TabIndex = 0;
            this.lvProps.View = View.Details;
            this.colName.Text = "Property";
            this.colName.Width = 120;
            this.colValue.Text = "Value";
            this.colValue.Width = 300;
            this.lblName.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.lblName.Location = new Point(8, 0x18);
            this.lblName.Name = "lblName";
            this.lblName.Size = new Size(0x11f, 0x30);
            this.lblName.TabIndex = 1;
            this.lblName.Text = "label1";
            this.lblKind.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Italic | FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblKind.ForeColor = SystemColors.ControlDark;
            this.lblKind.Location = new Point(8, 0);
            this.lblKind.Name = "lblKind";
            this.lblKind.Size = new Size(0x115, 15);
            this.lblKind.TabIndex = 2;
            this.lblKind.Text = "label1";
            base.Controls.AddRange(new Control[] { this.lblKind, this.lblName, this.lvProps });
            base.Name = "ObjViewer";
            base.Size = new Size(0x128, 0x170);
            base.ResumeLayout(false);
        }

        public void ReadNode(object obj, string title, string subtitle)
        {
            this.Clear();
            if (obj != null)
            {
                this.lblName.Text = subtitle;
                this.lblKind.Text = title;
                this.ShowObjectProps(obj);
            }
        }

        private void ShowObjectProps(object o)
        {
            System.Type type = o.GetType();
            PropertyInfo[] properties = type.GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                object obj2 = null;
                try
                {
                    object[] customAttributes = properties[i].GetCustomAttributes(typeof(ObjViewerAttribute), true);
                    obj2 = type.InvokeMember(properties[i].Name, BindingFlags.GetProperty, null, o, new object[0]);
                    if (customAttributes.Length == 0)
                    {
                        this.AddProp(properties[i].Name, (obj2 == null) ? "null" : obj2.ToString());
                    }
                    else
                    {
                        ObjViewerAttribute attribute = (ObjViewerAttribute) customAttributes[0];
                        if (!attribute.Hide)
                        {
                            string name;
                            if (attribute.Rename)
                            {
                                name = attribute.Name;
                            }
                            else
                            {
                                name = properties[i].Name;
                            }
                            string v = obj2.ToString();
                            if (attribute.Hex)
                            {
                                if (obj2 is short)
                                {
                                    v = "0x" + ((short) obj2).ToString("X");
                                }
                                if (obj2 is int)
                                {
                                    v = "0x" + ((int) obj2).ToString("X8");
                                }
                                if (obj2 is long)
                                {
                                    v = "0x" + ((long) obj2).ToString("X");
                                }
                                if (obj2 is ushort)
                                {
                                    v = "0x" + ((ushort) obj2).ToString("X");
                                }
                                if (obj2 is uint)
                                {
                                    v = "0x" + ((uint) obj2).ToString("X8");
                                }
                                if (obj2 is ulong)
                                {
                                    v = "0x" + ((ulong) obj2).ToString("X");
                                }
                            }
                            this.AddProp(name, v);
                        }
                    }
                }
                catch (Exception exception)
                {
                    obj2 = exception;
                }
            }
        }
    }
}
